"use strict";
cc._RF.push(module, '1b48cGCTZdCZZCQRqG55qLY', 'gameEngine');
// scripts/engines/gameEngine.ts

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.modules = exports.managers = exports.services = void 0;
var localStorageService_1 = require("./services/localStorageService");
var soundManager_1 = require("./managers/soundManager");
var ossService_1 = require("./services/ossService");
var miniGameService_1 = require("./services/miniGameService");
var userService_1 = require("./services/userService");
var gameManager_1 = require("./managers/gameManager");
var uiModule_1 = require("./modules/uiModule");
var gameHttpModule_1 = require("./modules/gameHttpModule");
var gameWebSocketModule_1 = require("./modules/gameWebSocketModule");
var logService_1 = require("./services/logService");
var engineConfig_1 = require("./configs/engineConfig");
var MainRoot_1 = require("../Game/MainRoot");
var ziManager_1 = require("./managers/ziManager");
var peopleArcheryManage_1 = require("./managers/peopleArcheryManage");
var CsvDataManage_1 = require("./managers/CsvDataManage");
var goldMinerManager_1 = require("./managers/goldMinerManager");
function services() {
    return O1HD.getServices();
}
exports.services = services;
function managers() {
    return O1HD.getManagers();
}
exports.managers = managers;
function modules() {
    return O1HD.getModules();
}
exports.modules = modules;
var gameEngine = /** @class */ (function () {
    function gameEngine() {
        //模块
        this.m_modules = {
            uiControlModule: new uiModule_1.default(),
            gameHttpModule: new gameHttpModule_1.default(),
            gameWebsocketModule: new gameWebSocketModule_1.default(),
        };
        //服务
        this.services = {
            localStorageSrv: new localStorageService_1.default(),
            ossSrv: new ossService_1.default(),
            miniGameSrv: new miniGameService_1.default(),
            userSrv: new userService_1.default(),
            logSrv: new logService_1.default(),
        };
        //管理
        this.managers = {
            soundMgr: new soundManager_1.default(),
            gameMgr: new gameManager_1.default(),
            ziMgr: new ziManager_1.default(),
            csvDataManager: new CsvDataManage_1.default(),
            peopleArcheryManager: new peopleArcheryManage_1.default(),
            goldMinerManager: new goldMinerManager_1.default(),
        };
        // console.log('<游戏主框架>');
        //重写部分API
        this.reDefinedApi();
    }
    gameEngine.prototype.getModules = function () {
        return this.m_modules;
    };
    gameEngine.prototype.getManagers = function () {
        return this.managers;
    };
    gameEngine.prototype.getServices = function () {
        return this.services;
    };
    gameEngine.prototype.loadAll = function () {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: 
                    // console.log('■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■');
                    // console.log(COMPANY_COPY);
                    // console.log('@version', CODE_VERSION);
                    // console.log('@time', FRAME_TIME);
                    // console.log('@copy', COMPANY_NAME);
                    // console.log('■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■');
                    //加载
                    // const startTime = Date.now();
                    // let t = startTime;
                    // console.log('★★★★★框架载入★★★★★');
                    //模块
                    // console.log(`${LINE_SPLIT_START_LEFT}加载所有模块${LINE_SPLIT_START_RIGHT}`);
                    return [4 /*yield*/, O1HD.loadAllModules()];
                    case 1:
                        // console.log('■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■');
                        // console.log(COMPANY_COPY);
                        // console.log('@version', CODE_VERSION);
                        // console.log('@time', FRAME_TIME);
                        // console.log('@copy', COMPANY_NAME);
                        // console.log('■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■');
                        //加载
                        // const startTime = Date.now();
                        // let t = startTime;
                        // console.log('★★★★★框架载入★★★★★');
                        //模块
                        // console.log(`${LINE_SPLIT_START_LEFT}加载所有模块${LINE_SPLIT_START_RIGHT}`);
                        _a.sent();
                        // console.log(`${LINE_SPLIT_END_LEFT}所有模块已载${LINE_SPLIT_END_RIGHT}`, Date.now() - t, 'ms');
                        // t = Date.now();
                        //先加载oss服务 因为配置在里面
                        // console.log(`${LINE_SPLIT_START_LEFT}加载OSS服务${LINE_SPLIT_START_RIGHT}`);
                        return [4 /*yield*/, this.getServices().ossSrv.loadService()];
                    case 2:
                        // console.log(`${LINE_SPLIT_END_LEFT}所有模块已载${LINE_SPLIT_END_RIGHT}`, Date.now() - t, 'ms');
                        // t = Date.now();
                        //先加载oss服务 因为配置在里面
                        // console.log(`${LINE_SPLIT_START_LEFT}加载OSS服务${LINE_SPLIT_START_RIGHT}`);
                        _a.sent();
                        // console.log(`${LINE_SPLIT_END_LEFT}OSS服务已载${LINE_SPLIT_END_RIGHT}`, Date.now() - t, 'ms');
                        return [4 /*yield*/, this.getServices().ossSrv.onLoadedService()];
                    case 3:
                        // console.log(`${LINE_SPLIT_END_LEFT}OSS服务已载${LINE_SPLIT_END_RIGHT}`, Date.now() - t, 'ms');
                        _a.sent();
                        // t = Date.now();
                        //服务
                        // console.log(`${LINE_SPLIT_START_LEFT}加载所有服务${LINE_SPLIT_START_RIGHT}`);
                        return [4 /*yield*/, this.loadServices()];
                    case 4:
                        // t = Date.now();
                        //服务
                        // console.log(`${LINE_SPLIT_START_LEFT}加载所有服务${LINE_SPLIT_START_RIGHT}`);
                        _a.sent();
                        // console.log(`${LINE_SPLIT_END_LEFT}所有服务已载${LINE_SPLIT_END_RIGHT}`, Date.now() - t, 'ms');
                        // t = Date.now();
                        //管理
                        // console.log(`${LINE_SPLIT_START_LEFT}加载所有管理${LINE_SPLIT_START_RIGHT}`);
                        return [4 /*yield*/, this.loadManagers()];
                    case 5:
                        // console.log(`${LINE_SPLIT_END_LEFT}所有服务已载${LINE_SPLIT_END_RIGHT}`, Date.now() - t, 'ms');
                        // t = Date.now();
                        //管理
                        // console.log(`${LINE_SPLIT_START_LEFT}加载所有管理${LINE_SPLIT_START_RIGHT}`);
                        _a.sent();
                        // console.log(`${LINE_SPLIT_END_LEFT}所有管理已载${LINE_SPLIT_END_RIGHT}`, Date.now() - t, 'ms');
                        // t = Date.now();
                        // console.log('★★★★★框架完毕★★★★★', Date.now() - startTime, 'ms (总耗时)');
                        return [4 /*yield*/, MainRoot_1.default.GameInit()];
                    case 6:
                        // console.log(`${LINE_SPLIT_END_LEFT}所有管理已载${LINE_SPLIT_END_RIGHT}`, Date.now() - t, 'ms');
                        // t = Date.now();
                        // console.log('★★★★★框架完毕★★★★★', Date.now() - startTime, 'ms (总耗时)');
                        _a.sent();
                        return [2 /*return*/, Promise.resolve()];
                }
            });
        });
    };
    gameEngine.prototype.emit = function (event) {
        var _a;
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        (_a = this.Canvas()).emit.apply(_a, __spreadArrays([event], args));
    };
    gameEngine.prototype.on = function (event, callback, target) {
        this.Canvas().on(event, callback, target);
    };
    gameEngine.prototype.off = function (event, callBack, target) {
        this.Canvas().off(event, callBack, target);
    };
    gameEngine.prototype.once = function (event, callback) {
        var _a;
        var args = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            args[_i - 2] = arguments[_i];
        }
        (_a = this.Canvas()).once.apply(_a, __spreadArrays([event, callback], args));
    };
    /**
     * return {cc.Component} 当前场景画布挂载的脚本
     * */
    gameEngine.prototype.Component = function () {
        return this.Canvas().getComponent(cc.Component);
    };
    /**
     * @return {cc.Node} 当前场景的画布节点
     * */
    gameEngine.prototype.Canvas = function () {
        return cc.find('Canvas');
    };
    /**
     * 释放没使用资源
     */
    gameEngine.prototype.releaseUnusedAssets = function () {
        try {
            //@ts-ignore
            cc.resources.releaseUnusedAssets();
        }
        catch (e) {
            console.warn('#498 警告 res unused release', e);
        }
    };
    //切换场景
    gameEngine.prototype.changeScene = function (sceneName, successCallback) {
        this.loadScene(sceneName, successCallback);
    };
    //加载并切换场景
    gameEngine.prototype.loadScene = function (sceneName, successCallback) {
        var _this = this;
        // console.error("更换场景")
        O1HD.emit(engineConfig_1.GameEvent.CHANGE_SCENE_BEFORE);
        cc.director.loadScene(sceneName, function () {
            O1HD.emit(engineConfig_1.GameEvent.CHANGE_SCENE_AFTER);
            _this.services && _this.services.miniGameSrv.getAppApiInstance && _this.services.miniGameSrv.getAppApiInstance() && _this.services.miniGameSrv.getAppApiInstance().WidgetfullScreen && _this.services.miniGameSrv.getAppApiInstance().WidgetfullScreen(); //屏幕适配
            successCallback && successCallback();
        });
    };
    gameEngine.prototype.loadAllModules = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, _b, _i, key, _c, _d, _e, key;
            return __generator(this, function (_f) {
                switch (_f.label) {
                    case 0:
                        _a = [];
                        for (_b in this.m_modules)
                            _a.push(_b);
                        _i = 0;
                        _f.label = 1;
                    case 1:
                        if (!(_i < _a.length)) return [3 /*break*/, 4];
                        key = _a[_i];
                        return [4 /*yield*/, this.m_modules[key].preLoadModules()];
                    case 2:
                        _f.sent();
                        _f.label = 3;
                    case 3:
                        _i++;
                        return [3 /*break*/, 1];
                    case 4:
                        _c = [];
                        for (_d in this.m_modules)
                            _c.push(_d);
                        _e = 0;
                        _f.label = 5;
                    case 5:
                        if (!(_e < _c.length)) return [3 /*break*/, 9];
                        key = _c[_e];
                        return [4 /*yield*/, this.m_modules[key].loadModule()];
                    case 6:
                        _f.sent();
                        return [4 /*yield*/, this.m_modules[key].onLoadedModule()];
                    case 7:
                        _f.sent();
                        _f.label = 8;
                    case 8:
                        _e++;
                        return [3 /*break*/, 5];
                    case 9: return [2 /*return*/, Promise.resolve()];
                }
            });
        });
    };
    gameEngine.prototype.reDefinedApi = function () {
    };
    gameEngine.prototype.loadManagers = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, _b, _i, k, mgr;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        _a = [];
                        for (_b in this.managers)
                            _a.push(_b);
                        _i = 0;
                        _c.label = 1;
                    case 1:
                        if (!(_i < _a.length)) return [3 /*break*/, 5];
                        k = _a[_i];
                        mgr = this.managers[k];
                        return [4 /*yield*/, mgr.loadManager()];
                    case 2:
                        _c.sent();
                        return [4 /*yield*/, mgr.onLoadedManager()];
                    case 3:
                        _c.sent();
                        _c.label = 4;
                    case 4:
                        _i++;
                        return [3 /*break*/, 1];
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    gameEngine.prototype.loadServices = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, _b, _i, k, srv;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        _a = [];
                        for (_b in this.services)
                            _a.push(_b);
                        _i = 0;
                        _c.label = 1;
                    case 1:
                        if (!(_i < _a.length)) return [3 /*break*/, 5];
                        k = _a[_i];
                        srv = this.services[k];
                        if (!!srv.isLoaded()) return [3 /*break*/, 4];
                        return [4 /*yield*/, srv.loadService()];
                    case 2:
                        _c.sent();
                        return [4 /*yield*/, srv.onLoadedService()];
                    case 3:
                        _c.sent();
                        _c.label = 4;
                    case 4:
                        _i++;
                        return [3 /*break*/, 1];
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    return gameEngine;
}());
//引擎主入口
function main() {
    try {
        window['O1HD'] = new gameEngine();
        return window['O1HD'];
    }
    catch (e) {
        console.error('主引擎创建失败', e);
    }
}
var O1HD = main();
exports.default = O1HD;
// export function main() {
//     try {
//         if (null == O1HD) {
//             O1HD = new gameEngine();
//         }
//     } catch (e) {
//         console.error('游戏主引擎创建失败', e);
//     }
// }
//
// let O1HD: gameEngine = null;
//
// export default O1HD;

cc._RF.pop();